package webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeleteSale
 */
public class DeleteSale extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ServletConfig config;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config=config;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	System.out.println("We are in Service method of DeleteSale");
	PrintWriter out = response.getWriter();
	
	String todelete=request.getParameter("deleteid");
	
	String msg=" ";
	// Connection conn = null;
			 String url = "jdbc:mysql://localhost:3306/";
			 String dbName = "webappschema";
			 String driver = "com.mysql.jdbc.Driver";
			 String dbusername = "root";
			 String dbpassword = "webapp";
			 List dataList = new ArrayList(); 
			 
			 try {Class.forName(driver).newInstance();
			 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
			 String strQuery = ("Delete from sale where Saleid="+ todelete );
			 Statement st = conn.createStatement();
			 int rs = st.executeUpdate(strQuery);
			 response.setContentType("text/html");
			 
			 if(rs > 0) 
			 {
				 out.println("<script type=\"text/javascript\">");
	    	 	   out.println("alert('Record successfully deleted');");
	    	 	   out.println("location='View.jsp';");
	    	 	   out.println("</script>");
				 
				 //msg = "Record Deleted Successfully";
			     
				 //response.setContentType("text/html");
				 //PrintWriter out1 = response.getWriter();
				 //out1.println("<font size = '6' color=red>" + msg + "</font>");
			        
			    }
			 st.close();
		 	 } catch (Exception e) {
		 	   e.printStackTrace();
		 	 }
    	 

    	  }    
}


